package com.b511.swpu.cementbackend.controller;

import com.b511.swpu.cementbackend.dto.input.CodeList;
import com.b511.swpu.cementbackend.dto.input.CodeValue;
import com.b511.swpu.cementbackend.dto.input.MasterDataInputDto;
import com.b511.swpu.cementbackend.entity.*;
import com.b511.swpu.cementbackend.repository.*;
import com.b511.swpu.cementbackend.util.R;
import io.swagger.annotations.Api;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebResult;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

@Api(value = "数据接收", tags = {"数据接收"})
@RestController
@RequestMapping("/dataReceive")
public class DataReceiveController {
    // 打印数据到日志
    protected static final Logger logger = LoggerFactory.getLogger(DataReceiveController.class);

    private final MAIN_WellRepository mainWellRepository;

    private final MAIN_CountryRepository mainCountryRepository;

    private final MAIN_BlockAreaRepository blockAreaRepository;

    private final MAIN_BlockStructureRepository blockStructureRepository;

    private final MAIN_DrillRepository drillRepository;

    private final MAIN_DrillToolRepository drillToolRepository;

    private final MAIN_FluidSystemRepository fluidSystemRepository;

    private final MAIN_HorizonRepository horizonRepository;

    private final MAIN_OilFieldRepository oilFieldRepository;

    private final MAIN_OperatorRepository operatorRepository;

    private final MAIN_OrganizationRepository organizationRepository;

    private final MAIN_PersonRepository personRepository;

    private final MAIN_RigRepository rigRepository;

    private final MAIN_ServicerRepository servicerRepository;

    private final BA_WellRepository wellRepository;

    public DataReceiveController(MAIN_CountryRepository mainCountryRepository,
                                 MAIN_BlockAreaRepository blockAreaRepository,
                                 MAIN_BlockStructureRepository blockStructureRepository,
                                 MAIN_DrillRepository drillRepository,
                                 MAIN_DrillToolRepository drillToolRepository,
                                 MAIN_FluidSystemRepository fluidSystemRepository,
                                 MAIN_HorizonRepository horizonRepository,
                                 MAIN_OilFieldRepository oilFieldRepository,
                                 MAIN_OperatorRepository operatorRepository,
                                 MAIN_OrganizationRepository organizationRepository,
                                 MAIN_PersonRepository personRepository,
                                 MAIN_RigRepository rigRepository,
                                 MAIN_ServicerRepository servicerRepository,
                                 MAIN_WellRepository mainWellRepository,
                                 BA_WellRepository wellRepository){
        this.mainCountryRepository = mainCountryRepository;
        this.blockAreaRepository = blockAreaRepository;
        this.blockStructureRepository = blockStructureRepository;
        this.drillRepository = drillRepository;
        this.drillToolRepository = drillToolRepository;
        this.fluidSystemRepository = fluidSystemRepository;
        this.horizonRepository = horizonRepository;
        this.oilFieldRepository = oilFieldRepository;
        this.operatorRepository = operatorRepository;
        this.organizationRepository = organizationRepository;
        this.personRepository = personRepository;
        this.rigRepository = rigRepository;
        this.servicerRepository = servicerRepository;
        this.mainWellRepository = mainWellRepository;
        this.wellRepository = wellRepository;
    }

    // 接收主数据系统的数据
    @PostMapping("/masterData")
    @Transactional
    public R getMasterData(HttpServletRequest request) throws IOException {

        StringBuilder sb = new StringBuilder();
        InputStream is = request.getInputStream();
        InputStreamReader isr = new InputStreamReader(is, "utf-8");
        BufferedReader br = new BufferedReader(isr);

        // 井别的对照表
        Map<String, String> wellSpacingMap = new HashMap<String,String>();
        wellSpacingMap.put("1", "生产井");
        wellSpacingMap.put("2", "评价井");
        wellSpacingMap.put("3", "滚动开发井");
        wellSpacingMap.put("4", "注水井");
        wellSpacingMap.put("5", "预探井");
        wellSpacingMap.put("6", "水源井");
        wellSpacingMap.put("7", "调整井");
        wellSpacingMap.put("8", "其他井别");




        // 井型对照表
        Map<String, String> wellTypeMap = new HashMap<String,String>();
        wellTypeMap.put("1", "直井");
        wellTypeMap.put("2", "定向井");
        wellTypeMap.put("3", "水平井");
        wellTypeMap.put("4", "其它");
        wellTypeMap.put("5", "多底井");
        wellTypeMap.put("6", "救援井");
        wellTypeMap.put("7", "开窗侧钻井");
        wellTypeMap.put("8", "水平分支井");
        wellTypeMap.put("9", "大位移井");
        wellTypeMap.put("10", "大斜度井");
        wellTypeMap.put("11", "分支井");
        wellTypeMap.put("12", "其它");




        String s = "" ;
        while((s=br.readLine())!=null){
            sb.append(s) ;
        }
        String reqBody = sb.toString();
        // logger.info("reqBody" + reqBody);

        // String -> JSONObject
        JSONObject reponseData=JSONObject.fromObject(reqBody);

        // 复杂类型转化解决方案：使用HashMap
        Map codeMap = new HashMap();
        codeMap.put("codelist", CodeList.class);

        // JSONObject -> Java对象
        MasterDataInputDto inputData = (MasterDataInputDto) JSONObject.toBean(reponseData,MasterDataInputDto.class, codeMap);
        List<CodeList> tmpCodeList = inputData.getCodelist();

        // codeList -> HashMap
        Map codeListMap = new HashMap<String,String>();
        for (CodeList codeList : tmpCodeList){
            codeListMap.put(codeList.getListname(), codeList.getListvalue());
        }

        String relationCode = codeListMap.get("唯一标识").toString();
        String mdmid = inputData.getMdmid();
        String operation = inputData.getOperation();

        // 复杂类型转化解决方案：使用HashMap
        Map valueMap = new HashMap();
        valueMap.put("codevalue", CodeValue.class);

        MasterDataInputDto codeValueData = (MasterDataInputDto) JSONObject.toBean(reponseData,MasterDataInputDto.class, valueMap);
        List<CodeValue> tmpCodeValue = codeValueData.getCodevalue();

        // codeValue -> HashMap
        Map codeValueMap = new HashMap<String,String>();
        for (CodeValue codeValueList : tmpCodeValue){
            codeValueMap.put(codeValueList.getPropertyname(), codeValueList.getPropertyvalue());
        }

        /**
         * relationCode : 全局唯一编码
         * tmpCodeList : 解析出的codelist值
         * tmpCodeValue : 解析出的codevalue值
         * mdmid : 解析出的mdmid值
         * operation : 解析出的operation值
         */

        // 根据mdmid判断是对那一张表进行操作
        // 对国别表进行操作 MAIN_Country
        if ("COUNTRY".equals(mdmid)){
            // TODO: 按照CountryCode进行删除和查找
            String countryCode = codeValueMap.get("国别代码").toString();
            if ("insert".equals(operation) || "update".equals(operation)){
                // 是否存在
                MAIN_Country exist = mainCountryRepository.findByCountryCode(countryCode);
                if (null != exist){
                    mainCountryRepository.deleteByCountryCode(countryCode);
                }
                MAIN_Country tmpCountry = new MAIN_Country();
                // 新增
                tmpCountry.setRelationCode(relationCode);// 关联编码
                tmpCountry.setCountryCode(codeValueMap.get("国别代码").toString());// 国别代码
                tmpCountry.setChineseName(codeValueMap.get("中文名称").toString());// 中文名称
                tmpCountry.setEnglishName(codeValueMap.get("英文名称").toString());// 英文名称
                mainCountryRepository.save(tmpCountry);
            }
            if ("delete".equals(operation)){
                // 删除
                mainCountryRepository.deleteByCountryCode(countryCode);
            }
        }

        // 井数据表 MAIN_WELL
        if ("WELL".equals(mdmid)) {
            if ("insert".equals(operation) || "update".equals(operation)){
                // TODO: 通过井编码找唯一的井
                String wellCode = codeValueMap.get("井编码").toString() ;
                MAIN_WELL exist = mainWellRepository.findByWellCode(wellCode);
                if (null != exist){
                    mainWellRepository.deleteByWellCode(wellCode);
                }

                // 新增 MAIN_WELL 表的记录
                MAIN_WELL tmpWell = new MAIN_WELL();
                tmpWell.setRelationCode(relationCode); // 关联编码
                if (codeValueMap.containsKey("井编码")){
                    tmpWell.setWellCode(codeValueMap.get("井编码").toString());// 井编码
                }
                if (codeValueMap.containsKey("井名")){
                    tmpWell.setWellName(codeValueMap.get("井名").toString());// 井名
                }
                if (codeValueMap.containsKey("主井眼名")){
                    tmpWell.setBoreholeName(codeValueMap.get("主井眼名").toString());// 主井眼名
                }
                if (codeValueMap.containsKey("开窗深度")){
                    tmpWell.setOpenWindowDepth(codeValueMap.get("开窗深度").toString());// 开窗深度
                }
                if (codeValueMap.containsKey("开窗时间")){
                    tmpWell.setOpenWindowTime(codeValueMap.get("开窗时间").toString());// 开窗时间
                }
                if (codeValueMap.containsKey("国别")){
                    tmpWell.setCountry(codeValueMap.get("国别").toString());// 国别
                }
                if (codeValueMap.containsKey("区域")){
                    tmpWell.setArea(codeValueMap.get("区域").toString());// 区域
                }
                if (codeValueMap.containsKey("所属区块/构造")){
                    tmpWell.setBlock(codeValueMap.get("所属区块/构造").toString());// 所属区块/构造
                }
                if (codeValueMap.containsKey("所属油田")){
                    tmpWell.setOilField(codeValueMap.get("所属油田").toString());// 所属油田
                }
                if (codeValueMap.containsKey("井型")){
                    Set<String> wellTypKeys = wellTypeMap.keySet();
                    for (String key : wellTypKeys) {
                        if(key.equals(codeValueMap.get("井型").toString())){
                            tmpWell.setWellCategory(wellTypeMap.get(key));// 井型
                        }
                    }

//                    tmpWell.setWellCategory(codeValueMap.get("井型").toString());// 井型
                }
                if (codeValueMap.containsKey("井别")){
                    Set<String> wellCategoryKeys = wellSpacingMap.keySet();
                    for (String key : wellCategoryKeys) {
                        if(key.equals(codeValueMap.get("井别").toString())){
                            tmpWell.setWellType(wellSpacingMap.get(key));// 井别
                        }
                    }
//                    tmpWell.setWellType(codeValueMap.get("井别").toString());// 井别
                }
                if (codeValueMap.containsKey("井类")){
                    tmpWell.setWellClass(codeValueMap.get("井类").toString());// 井类
                }
                if (codeValueMap.containsKey("设计井深")){
                    tmpWell.setDesignDepth(codeValueMap.get("设计井深").toString());// 设计井深
                }
                if (codeValueMap.containsKey("是否陆地井")){
                    tmpWell.setIsLandWell(codeValueMap.get("是否陆地井").toString());// 是否陆地井
                }
                if (codeValueMap.containsKey("钻井公司")){
                    tmpWell.setDrillCompany(codeValueMap.get("钻井公司").toString());// 钻井公司
                }
                if (codeValueMap.containsKey("钻井液作业公司")){
                    tmpWell.setDrillFluidCompany(codeValueMap.get("钻井液作业公司").toString());// 钻井液作业公司
                }
                if (codeValueMap.containsKey("固井作业公司")){
                    tmpWell.setCementCompany(codeValueMap.get("固井作业公司").toString());// 固井作业公司
                }
                if (codeValueMap.containsKey("EPS作业公司")){
                    tmpWell.setEPSCompany(codeValueMap.get("EPS作业公司").toString());// EPS作业公司
                }
                if (codeValueMap.containsKey("承包商")){
                    tmpWell.setContractor(codeValueMap.get("承包商").toString());// 承包商
                }
                if (codeValueMap.containsKey("承包商代表")){
                    tmpWell.setContractorRepresentative(codeValueMap.get("承包商代表").toString());// 承包商代表
                }
                if (codeValueMap.containsKey("作业者")){
                    tmpWell.setOperator(codeValueMap.get("作业者").toString());// 作业者
                }
                if (codeValueMap.containsKey("作业者代表")){
                    tmpWell.setOperatorRepresentative(codeValueMap.get("作业者代表").toString());// 作业者代表
                }
                if (codeValueMap.containsKey("所属平台")){
                    tmpWell.setPlantform(codeValueMap.get("所属平台").toString());// 所属平台
                }
                if (codeValueMap.containsKey("钻机编码")){
                    tmpWell.setDrillCode(codeValueMap.get("钻机编码").toString());// 钻机编码
                }

                mainWellRepository.save(tmpWell);

                // 同步 BA_WELL
                BA_Well existWell = wellRepository.findByWellName(codeValueMap.get("井名").toString());

                if( null != existWell ){
                    // update
                    if (codeValueMap.containsKey("井名")){
                        if(!("").equals(codeValueMap.get("井名").toString())){
                            existWell.setWellName(codeValueMap.get("井名").toString());
                        }
                    }
                    if (codeValueMap.containsKey("国别")){
                        if(!("").equals(codeValueMap.get("国别").toString())){
                            existWell.setCountry(codeValueMap.get("国别").toString());
                        }
                    }
                    if (codeValueMap.containsKey("区域")) {
                        if(!("").equals(codeValueMap.get("区域").toString())){
                            existWell.setArea(codeValueMap.get("区域").toString());
                        }
                    }
                    if (codeValueMap.containsKey("所属区块/构造")) {
                        if(!("").equals(codeValueMap.get("所属区块/构造").toString())){
                            existWell.setBlock(codeValueMap.get("所属区块/构造").toString());
                        }
                    }
                    if (codeValueMap.containsKey("所属油田")) {
                        if(!("").equals(codeValueMap.get("所属油田").toString())){
                            existWell.setOilField(codeValueMap.get("所属油田").toString());
                        }
                    }
                    if (codeValueMap.containsKey("井型")) {
                        if(!("").equals(codeValueMap.get("井型").toString())){
                            Set<String> wellTypKeys = wellTypeMap.keySet();
                            for (String key : wellTypKeys) {
                                if(key.equals(codeValueMap.get("井型").toString())){
                                    existWell.setWellType(wellTypeMap.get(key));// 井型
                                }
                            }

                        }
                    }
                    if (codeValueMap.containsKey("井别")) {
                        if(!("").equals(codeValueMap.get("井别").toString())){
                            Set<String> wellCategoryKeys = wellSpacingMap.keySet();
                            for (String key : wellCategoryKeys) {
                                if(key.equals(codeValueMap.get("井别").toString())){
                                    existWell.setWellSpacing(wellSpacingMap.get(key));// 井别
                                }
                            }

                        }
                    }
                    if (codeValueMap.containsKey("井类")) {
                        if(!("").equals(codeValueMap.get("井类").toString())){
                            existWell.setWellCategory(codeValueMap.get("井类").toString());// 井类
                        }
                    }
                    if (codeValueMap.containsKey("设计井深")) {
                        if(!("").equals(codeValueMap.get("设计井深").toString())){
                            existWell.setDesignDepth(Double.valueOf(codeValueMap.get("设计井深").toString()));
                        }
                    }
                    if (codeValueMap.containsKey("是否陆地井")) {
                        if(!("").equals(codeValueMap.get("是否陆地井").toString())){
                            existWell.setIsLandWell(codeValueMap.get("是否陆地井").toString());
                        }
                    }
                    if (codeValueMap.containsKey("钻井公司")) {
                        if(!("").equals(codeValueMap.get("钻井公司").toString())){
                            existWell.setBoreholeCompany(codeValueMap.get("钻井公司").toString());// 钻井公司
                        }
                    }
                    if (codeValueMap.containsKey("钻井液作业公司")) {
                        if(!("").equals(codeValueMap.get("钻井液作业公司").toString())){
                            existWell.setMudCompany(codeValueMap.get("钻井液作业公司").toString());// 钻井液作业公司
                        }
                    }
                    if (codeValueMap.containsKey("固井作业公司")) {
                        if(!("").equals(codeValueMap.get("固井作业公司").toString())){
                            existWell.setCementCompany(codeValueMap.get("固井作业公司").toString());// 固井作业公司
                        }
                    }
                    if (codeValueMap.containsKey("作业者")) {
                        if(!("").equals(codeValueMap.get("作业者").toString())){
                            existWell.setOperator(codeValueMap.get("作业者").toString());// 作业者
                        }
                    }
                    if (codeValueMap.containsKey("作业者代表")) {
                        if(!("").equals(codeValueMap.get("作业者代表").toString())){
                            existWell.setOperatorDeputy(codeValueMap.get("作业者代表").toString());// 作业者代表
                        }
                    }
                    if (codeValueMap.containsKey("所属平台")) {
                        if(!("").equals(codeValueMap.get("所属平台").toString())){
                            existWell.setDrillingPlatform(codeValueMap.get("所属平台").toString());// 所属平台
                        }
                    }

                    wellRepository.save(existWell);
                }else {
                    // insert
                    BA_Well addedWell = new BA_Well();

                    if (codeValueMap.containsKey("井名")){
                        if(!("").equals(codeValueMap.get("井名").toString())){
                            addedWell.setWellName(codeValueMap.get("井名").toString());
                        }
                    }
                    if (codeValueMap.containsKey("国别")){
                        if(!("").equals(codeValueMap.get("国别").toString())){
                            addedWell.setCountry(codeValueMap.get("国别").toString());
                        }
                    }
                    if (codeValueMap.containsKey("区域")) {
                        if(!("").equals(codeValueMap.get("区域").toString())){
                            addedWell.setArea(codeValueMap.get("区域").toString());
                        }
                    }
                    if (codeValueMap.containsKey("所属区块/构造")) {
                        if(!("").equals(codeValueMap.get("所属区块/构造").toString())){
                            addedWell.setBlock(codeValueMap.get("所属区块/构造").toString());
                        }
                    }
                    if (codeValueMap.containsKey("所属油田")) {
                        if(!("").equals(codeValueMap.get("所属油田").toString())){
                            addedWell.setOilField(codeValueMap.get("所属油田").toString());
                        }
                    }
                    if (codeValueMap.containsKey("井型")) {
                        if(!("").equals(codeValueMap.get("井型").toString())){
                            Set<String> wellTypKeys = wellTypeMap.keySet();
                            for(String key : wellTypKeys) {
                                if (key.equals(codeValueMap.get("井型").toString())) {
                                    addedWell.setWellType(wellTypeMap.get(key));// 井型

                                }
                            }

                        }
                    }
                    if (codeValueMap.containsKey("井别")) {
                        if(!("").equals(codeValueMap.get("井别").toString())){
                            Set<String> wellCategoryKeys = wellSpacingMap.keySet();
                            for(String key : wellCategoryKeys){
                                if(key.equals(codeValueMap.get("井别").toString())){
                                    addedWell.setWellSpacing(wellSpacingMap.get(key));// 井别

                                }
                            }
                        }
                    }
                    if (codeValueMap.containsKey("井类")) {
                        if(!("").equals(codeValueMap.get("井类").toString())){
                            addedWell.setWellCategory(codeValueMap.get("井类").toString());// 井类
                        }
                    }
                    if (codeValueMap.containsKey("设计井深")) {
                        if(!("").equals(codeValueMap.get("设计井深").toString())){
                            addedWell.setDesignDepth(Double.valueOf(codeValueMap.get("设计井深").toString()));
                        }
                    }
                    if (codeValueMap.containsKey("是否陆地井")) {
                        if(!("").equals(codeValueMap.get("是否陆地井").toString())){
                            addedWell.setIsLandWell(codeValueMap.get("是否陆地井").toString());
                        }
                    }
                    if (codeValueMap.containsKey("钻井公司")) {
                        if(!("").equals(codeValueMap.get("钻井公司").toString())){
                            addedWell.setBoreholeCompany(codeValueMap.get("钻井公司").toString());// 钻井公司
                        }
                    }
                    if (codeValueMap.containsKey("钻井液作业公司")) {
                        if(!("").equals(codeValueMap.get("钻井液作业公司").toString())){
                            addedWell.setMudCompany(codeValueMap.get("钻井液作业公司").toString());// 钻井液作业公司
                        }
                    }
                    if (codeValueMap.containsKey("固井作业公司")) {
                        if(!("").equals(codeValueMap.get("固井作业公司").toString())){
                            addedWell.setCementCompany(codeValueMap.get("固井作业公司").toString());// 固井作业公司
                        }
                    }
                    if (codeValueMap.containsKey("作业者")) {
                        if(!("").equals(codeValueMap.get("作业者").toString())){
                            addedWell.setOperator(codeValueMap.get("作业者").toString());// 作业者
                        }
                    }
                    if (codeValueMap.containsKey("作业者代表")) {
                        if(!("").equals(codeValueMap.get("作业者代表").toString())){
                            addedWell.setOperatorDeputy(codeValueMap.get("作业者代表").toString());// 作业者代表
                        }
                    }
                    if (codeValueMap.containsKey("所属平台")) {
                        if(!("").equals(codeValueMap.get("所属平台").toString())){
                            addedWell.setDrillingPlatform(codeValueMap.get("所属平台").toString());// 所属平台
                        }
                    }
                    wellRepository.save(addedWell);
                }
            }
            if ("delete".equals(operation)){
                String wellCode = codeValueMap.get("井编码").toString() ;
                // 删除 MAIN_WELL
                mainWellRepository.deleteByWellCode(wellCode);
                // 同步删除 BA_WELL if exist
                String wellName = codeValueMap.get("井名").toString();
                BA_Well existWell = wellRepository.findByWellName(wellName);
                if (null != existWell){
                    wellRepository.deleteByWellName(wellName);
                }
            }
        }

        // 区块海域数据表 AREA
        if ("AREA".equals(mdmid)) {
            // TODO: 按照baCode进行查找和删除
            String baCode = codeValueMap.get("海域/区域编码").toString();
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_BlockArea exist = blockAreaRepository.findByBaCode(baCode);
                if (null != exist){
                    blockAreaRepository.deleteByBaCode(baCode);
                }

                // 新增
                MAIN_BlockArea tmpBlockArea = new MAIN_BlockArea();
                tmpBlockArea.setRelationCode(relationCode);// 关联编码
                tmpBlockArea.setBaCode(codeValueMap.get("海域/区域编码").toString());// 海域/区域编码
                tmpBlockArea.setBsName(codeValueMap.get("名称").toString());// 名称
                tmpBlockArea.setCountry(codeValueMap.get("所属国别").toString());// 所属国别
                blockAreaRepository.save(tmpBlockArea);
            }
            if ("delete".equals(operation)){
                // 删除
                blockAreaRepository.deleteByBaCode(baCode);
            }
        }

        // 区块构造表 BLOCK_TECTONIC
        if ("BLOCK_TECTONIC".equals(mdmid)) {
            // TODO: 按照 BSCODE进行删除和查找
            String bsCode = codeValueMap.get("区域/构造编码").toString();
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_BlockStructure exist = blockStructureRepository.findByBsCode(bsCode);
                if (null != exist){
                    blockStructureRepository.deleteByBsCode(bsCode);
                }

                // 新增
                MAIN_BlockStructure tmpBlockStructure = new MAIN_BlockStructure();
                tmpBlockStructure.setRelationCode(relationCode);// 关联编码
                tmpBlockStructure.setBsCode(codeValueMap.get("区域/构造编码").toString());// 区域/构造编码
                tmpBlockStructure.setBsName(codeValueMap.get("区域/构造名称").toString());// 区域/构造名称
                tmpBlockStructure.setArea(codeValueMap.get("所属区（海）域").toString());// 所属区（海）域
                tmpBlockStructure.setLocation(codeValueMap.get("地理位置").toString());// 地理位置
                tmpBlockStructure.setCompanyId(codeValueMap.get("所属公司标识").toString());// 所属公司标识
                tmpBlockStructure.setCountry(codeValueMap.get("国别").toString());// 国别
                tmpBlockStructure.setIsLand(codeValueMap.get("是否陆地").toString());// 是否陆地
                blockStructureRepository.save(tmpBlockStructure);
            }
            if ("delete".equals(operation)){
                // 删除
                blockStructureRepository.deleteByBsCode(bsCode);
            }
        }

        // 钻头数据表 DRILL_BIT
        if ("DRILL_BIT".equals(mdmid)) {
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_Drill exist = drillRepository.findByRelationCode(relationCode);
                if (null != exist){
                    drillRepository.deleteByRelationCode(relationCode);
                }

                // 新增
                MAIN_Drill tmpDrill = new MAIN_Drill();
                tmpDrill.setRelationCode(relationCode);// 关联编码
                tmpDrill.setDrillType(codeValueMap.get("类型").toString());// 类型
                tmpDrill.setDrillSize(codeValueMap.get("尺寸").toString());// 尺寸
                drillRepository.save(tmpDrill);
            }
            if ("delete".equals(operation)){
                // 删除
                drillRepository.deleteByRelationCode(relationCode);
            }
        }

        // 钻具数据表 DRILL_TOOL
        if ("DRILL_TOOL".equals(mdmid)) {
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_DrillTool exist = drillToolRepository.findByRelationCode(relationCode);
                if (null != exist){
                    drillToolRepository.deleteByRelationCode(relationCode);
                }

                // 新增
                MAIN_DrillTool tmpDrillTool = new MAIN_DrillTool();
                tmpDrillTool.setRelationCode(relationCode);// 关联编码
                tmpDrillTool.setCode(codeValueMap.get("钻具编码").toString());// 钻具编码
                tmpDrillTool.setCaseSize(codeValueMap.get("尺寸").toString());// 尺寸
                tmpDrillTool.setValidLength(codeValueMap.get("有效长度").toString());// 有效长度
                tmpDrillTool.setFullLength(codeValueMap.get("全长").toString());// 全长
                tmpDrillTool.setOutSize(codeValueMap.get("外径").toString());// 外径
                tmpDrillTool.setInSize(codeValueMap.get("内径").toString());// 内径
                tmpDrillTool.setVol(codeValueMap.get("容量").toString());// 容量
                tmpDrillTool.setContactType(codeValueMap.get("接头类型").toString());// 接头类型
                tmpDrillTool.setWeight(codeValueMap.get("标称重量").toString());// 标称重量
                tmpDrillTool.setCaseThickness(codeValueMap.get("套管厚度").toString());// 套管厚度
                tmpDrillTool.setGrade(codeValueMap.get("等级").toString());// 等级
                tmpDrillTool.setThreadType(codeValueMap.get("螺纹类型").toString());// 螺纹类型
                tmpDrillTool.setDisplacement(codeValueMap.get("排量").toString());// 排量
                tmpDrillTool.setTotalDisplacement(codeValueMap.get("总排量").toString());// 总排量
                tmpDrillTool.setContactInSize(codeValueMap.get("接头内径").toString());// 接头内径
                tmpDrillTool.setContactOutSize(codeValueMap.get("接头外径").toString());// 接头外径
                tmpDrillTool.setCaseCategory(codeValueMap.get("类别").toString());// 类别
                drillToolRepository.save(tmpDrillTool);
            }
            if ("delete".equals(operation)){
                // 删除
                drillToolRepository.deleteByRelationCode(relationCode);
            }
        }

        // 钻井液体系数据表 DRILLFLUID_SYSTEM
        if ("DRILLFLUID_SYSTEM".equals(mdmid)) {
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_FluidSystem exist = fluidSystemRepository.findByRelationCode(relationCode);
                if(null != exist){
                    fluidSystemRepository.deleteByRelationCode(relationCode);
                }
                // 新增
                MAIN_FluidSystem tmpFluidSystem = new MAIN_FluidSystem();
                tmpFluidSystem.setRelationCode(relationCode);// 关联编码
                tmpFluidSystem.setCode(codeValueMap.get("体系编码").toString());// 体系编码
                tmpFluidSystem.setName(codeValueMap.get("名称").toString());// 名称
                tmpFluidSystem.setType(codeValueMap.get("类别").toString());// 类别
                fluidSystemRepository.save(tmpFluidSystem);
            }
            if ("delete".equals(operation)){
                // 删除
                fluidSystemRepository.deleteByRelationCode(relationCode);
            }
        }

        // 层位表 HORIZON
        if ("HORIZON".equals(mdmid)) {
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_Horizon exist = horizonRepository.findByRelationCode(relationCode);
                if(null != exist){
                    horizonRepository.deleteByRelationCode(relationCode);
                }

                // 新增
                MAIN_Horizon tmpHorizon = new MAIN_Horizon();
                tmpHorizon.setRelationCode(relationCode);// 关联编码
                tmpHorizon.setHorizonCode(codeValueMap.get("层位代码").toString());// 层位代码
                tmpHorizon.setSerial(codeValueMap.get("系").toString());// 系
                tmpHorizon.setTong(codeValueMap.get("统").toString());// 统
                tmpHorizon.setSection(codeValueMap.get("段").toString());// 段
                tmpHorizon.setFormation(codeValueMap.get("组").toString());// 组
                tmpHorizon.setArea(codeValueMap.get("地（海）域").toString());// 地（海）域
                horizonRepository.save(tmpHorizon);
            }
            if ("delete".equals(operation)){
                // 删除
                horizonRepository.deleteByRelationCode(relationCode);
            }
        }

        // 油田数据表 OILFIELD
        if ("OILFIELD".equals(mdmid)) {
            // TODO: 按照OilCode进行查找和删除
            String oilCode = codeValueMap.get("油田编码").toString();
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_OilField exist = oilFieldRepository.findByOilCode(oilCode);
                if(null != exist){
                    oilFieldRepository.deleteByOilCode(oilCode);
                }

                // 新增
                MAIN_OilField tmpOilField = new MAIN_OilField();
                tmpOilField.setRelationCode(relationCode);// 关联编码
                tmpOilField.setOilCode(codeValueMap.get("油田编码").toString());// 油田编码
                tmpOilField.setOilName(codeValueMap.get("油田名称").toString());// 油田名称
                tmpOilField.setArea(codeValueMap.get("所属区（海）域").toString());// 所属区（海）域
                tmpOilField.setLocation(codeValueMap.get("地理位置").toString());// 地理位置
                tmpOilField.setWellHeadLongitude(codeValueMap.get("经度").toString());// 经度
                tmpOilField.setWellHeadLatitude(codeValueMap.get("纬度").toString());// 纬度
                tmpOilField.setCompanyId(codeValueMap.get("所属公司标识").toString());// 所属公司标识
                tmpOilField.setCountry(codeValueMap.get("国别").toString());// 国别
                tmpOilField.setIsLand(codeValueMap.get("是否陆地").toString());// 是否陆地
                tmpOilField.setBlock(codeValueMap.get("所属区块/构造").toString());// 所属区块/构造
                oilFieldRepository.save(tmpOilField);
            }
            if ("delete".equals(operation)){
                // 删除
                oilFieldRepository.deleteByOilCode(oilCode);
            }
        }

        // 作业者表 OPERATOR
        if ("OPERATOR".equals(mdmid)){
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_Operator exist = operatorRepository.findByRelationCode(relationCode);
                if(null != exist){
                    operatorRepository.deleteByRelationCode(relationCode);
                }

                // 新增
                MAIN_Operator tmpOperator = new MAIN_Operator();
                tmpOperator.setRelationCode(relationCode);// 关联编码
                tmpOperator.setCode(codeValueMap.get("作业者编码").toString());// 作业者编码
                tmpOperator.setName(codeValueMap.get("作业者名称").toString());// 作业者名称
                tmpOperator.setOperatorRepresent(codeValueMap.get("作业者代表").toString());// 作业者代表
                tmpOperator.setCountry(codeValueMap.get("国别").toString());// 国别
                operatorRepository.save(tmpOperator);
            }
            if ("delete".equals(operation)){
                // 删除
                operatorRepository.deleteByRelationCode(relationCode);
            }
        }

        // 组织表 ORG
        if ("ORG".equals(mdmid)){
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_Organization exist = organizationRepository.findByRelationCode(relationCode);
                if(null != exist){
                    organizationRepository.deleteByRelationCode(relationCode);
                }

                // 新增
                MAIN_Organization tmpOrg = new MAIN_Organization();
                tmpOrg.setRelationCode(relationCode);// 关联编码
                tmpOrg.setCode(codeValueMap.get("组织编码").toString());// 组织编码
                tmpOrg.setName(codeValueMap.get("组织名称").toString());// 组织名称
                tmpOrg.setParentCode(codeValueMap.get("上级组织编码").toString());// 上级组织编码
                tmpOrg.setLayer(codeValueMap.get("组织层次").toString());// 组织层次
                organizationRepository.save(tmpOrg);
            }
            if ("delete".equals(operation)){
                // 删除
                organizationRepository.deleteByRelationCode(relationCode);
            }
        }

        // 人员表 USER
        if ("USER".equals(mdmid)) {
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_Person exist = personRepository.findByRelationCode(relationCode);
                if(null != exist){
                    personRepository.deleteByRelationCode(relationCode);
                }

                // 新增
                MAIN_Person tmpPerson = new MAIN_Person();
                tmpPerson.setRelationCode(relationCode);// 关联编码
                tmpPerson.setCode(codeValueMap.get("员工编码").toString());// 员工编码
                tmpPerson.setName(codeValueMap.get("姓名").toString());// 姓名
                tmpPerson.setSex(codeValueMap.get("性别").toString());// 性别
                tmpPerson.setNation(codeValueMap.get("民族").toString());// 民族
                tmpPerson.setIdNo(codeValueMap.get("身份证号").toString());// 身份证号
                tmpPerson.setMobilePhone(codeValueMap.get("手机号码").toString());// 手机号码
                tmpPerson.setOfficePhone(codeValueMap.get("办公电话").toString());// 办公电话
                tmpPerson.setEmail(codeValueMap.get("邮箱").toString());// 邮箱
                tmpPerson.setCompanyName(codeValueMap.get("单位名称").toString());// 单位名称
                tmpPerson.setDepartmentName(codeValueMap.get("所在部门").toString());// 所在部门
                tmpPerson.setGroupCode(codeValueMap.get("所属组织编码").toString());// 所在组织编码
                personRepository.save(tmpPerson);
            }
            if ("delete".equals(operation)){
                // 删除
                personRepository.deleteByRelationCode(relationCode);
            }
        }

        // 钻机表 RIG
        if ("RIG".equals(mdmid) ) {
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_Rig exist = rigRepository.findByRelationCode(relationCode);
                if(null != exist){
                    rigRepository.deleteByRelationCode(relationCode);
                }

                // 新增
                MAIN_Rig tmpRig = new MAIN_Rig();
                tmpRig.setRelationCode(relationCode);// 关联编码
                tmpRig.setCode(codeValueMap.get("钻机编码").toString());// 钻机编码
                tmpRig.setName(codeValueMap.get("名称").toString());// 名称
                tmpRig.setLocation(codeValueMap.get("位置").toString());// 位置
                tmpRig.setWellHeadLongitude(codeValueMap.get("经度").toString());// 经度
                tmpRig.setWellHeadLatitude(codeValueMap.get("纬度").toString());// 纬度
                tmpRig.setType(codeValueMap.get("类型").toString());// 类型
                rigRepository.save(tmpRig);
            }
            if ("delete".equals(operation)){
                // 删除
                rigRepository.deleteByRelationCode(relationCode);
            }
        }

        // 服务商表 SERVICER
        if ("SERVICER".equals(mdmid)) {
            if ("insert".equals(operation) || "update".equals(operation)){
                MAIN_Servicer exist = servicerRepository.findByRelationCode(relationCode);
                if(null != exist){
                    servicerRepository.deleteByRelationCode(relationCode);
                }

                // 新增
                MAIN_Servicer tmpService = new MAIN_Servicer();
                tmpService.setRelationCode(relationCode);// 关联编码
                tmpService.setServicerCode(codeValueMap.get("服务商编码").toString());// 服务商编码
                tmpService.setServicerName(codeValueMap.get("服务商名称").toString());// 服务商名称
                tmpService.setServicerDelegate(codeValueMap.get("服务商代表").toString());// 服务商代表
                tmpService.setServicerTell(codeValueMap.get("服务商电话").toString());// 服务商电话
                tmpService.setServicerAddress(codeValueMap.get("服务商地址").toString());// 服务商地址
                tmpService.setCos(codeValueMap.get("服务类别").toString());// 服务类别
                servicerRepository.save(tmpService);
            }
            if ("delete".equals(operation)){
                // 删除
                servicerRepository.deleteByRelationCode(relationCode);
            }
        }

        return R.ok().putData("");
    }
}
